package com.heatup.apt.management.controller;

import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.heatup.apt.common.util.StringUtil;
import com.heatup.apt.management.service.SysUserService;
import com.heatup.apt.management.service.store.StoreService;
import com.heatup.apt.model.management.SysMenu;
import com.heatup.apt.model.management.SysRole;
import com.heatup.apt.model.management.SysUser;
import com.heatup.apt.model.store.StoreInfo;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 权限-用户相关操作
 * <P>File name : UserController.java </P>
 * <P>Author : fangxiaowen </P> 
 * <P>Date : 2016年10月8日 </P>
 */
@Controller
@RequestMapping("/system/user")
public class UserController extends BaseController{

	@Resource
	private SysUserService sysUserService;
	
	@Resource
	private StoreService storeService;
	
	/**
	 * 跳转到用户列表
	 * UserController.to_user_list()<BR>
	 * <P>Author :  fangxiaowen </P>  
	 * <P>Date : 2016年9月27日 </P>
	 * @param request
	 * @param response
	 * @return
	 */
    @RequestMapping(value = "to_user_list")
    public ModelAndView to_user_list(HttpServletRequest request, HttpServletResponse response) {
    	ModelAndView model = new ModelAndView();
    	
    	List<SysRole> role_list = sysUserService.queryAllRole();
    	request.setAttribute("role_list", role_list);
    	
        model.setViewName("user/user_list");
        return model;
    }
    
    /**
     * 跳转到新增用户 ·
     * UserController.to_user_add()<BR>
     * <P>Author :  fangxiaowen </P>  
     * <P>Date : 2016年9月30日 </P>
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "to_user_add")
    public ModelAndView to_user_add(HttpServletRequest request, HttpServletResponse response) {
    	ModelAndView model = new ModelAndView();
    	List<SysMenu> menu_list = sysUserService.queryMenuList();
    	request.setAttribute("menu_list", menu_list);
    	List<StoreInfo> storeList = storeService.queryAllOnlineStore();
    	List<SysRole> role_list = sysUserService.queryAllRole();
    	request.setAttribute("role_list", role_list);
    	request.setAttribute("storeList", storeList);
    	model.setViewName("user/user_add");
    	return model;
    }
    
    /**
     * 用户列表
     * UserController.user_list()<BR>
     * <P>Author :  fangxiaowen </P>  
     * <P>Date : 2016年9月27日 </P>
     * @param request
     * @param response
     */
    @RequestMapping(value = "user_list")
    public void user_list(HttpServletRequest request, HttpServletResponse response) {
    	Object pageIndex = request.getParameter("pageIndex");
    	Object pageSize = request.getParameter("pageSize");
    	if (StringUtil.isEmpty(pageIndex)) {
    		pageIndex = 0;
		}
    	if (StringUtil.isEmpty(pageSize)) {
    		pageSize = 10;
    	}
    	int page = Integer.valueOf(pageIndex.toString());
    	if (Integer.valueOf(pageIndex.toString()) > 0) {
			page = page - 1;
		}
    	String nickName = request.getParameter("nickName");
    	String mobile = request.getParameter("mobile");
    	String status = request.getParameter("status");
    	
    	Map<Object, Object> map = sysUserService.queryUserList(page, Integer.valueOf(pageSize.toString()),nickName,mobile,status);
    	JSONObject jsonObject = new JSONObject();
    	jsonObject.accumulate("user_list", map.get("user_list"));
    	jsonObject.accumulate("user_count", map.get("user_count"));
    	responseSendMsg(response, jsonObject.toString());
    }
   
    
    /**
     * 添加用户
     * UserController.user_add()<BR>
     * <P>Author :  fangxiaowen </P>  
     * <P>Date : 2016年10月8日 </P>
     * @param request
     * @param response
     */
    @RequestMapping(value = "user_add",method = {RequestMethod.POST})
    public void user_add(HttpServletRequest request, HttpServletResponse response) {
    	try {
    		request.setCharacterEncoding("UTF-8"); 
    		int count = 0;
        	String userName = request.getParameter("userName");
        	String mobile = request.getParameter("mobile");
        	String password = request.getParameter("password");
        	String menusId = request.getParameter("menusId");
        	Integer roleId = Integer.valueOf(request.getParameter("account").toString());
        	Integer storeId = Integer.valueOf(request.getParameter("storeId").toString());
        	
    		count = sysUserService.addUser(userName, mobile, password, roleId,getUserInfo().getId(), menusId, storeId);
    		responseSendMsg(response, count + "");
		} catch (Exception e) {
			e.printStackTrace();
			responseSendMsg(response, "添加失败!");
		}
    }


	/**
	 * 编辑保存
	 *
	 * @param sysUser
	 * @param response
	 */
	@RequestMapping(value = "user_edit",method = {RequestMethod.POST})
    public void user_edit(SysUser sysUser,HttpServletRequest request, HttpServletResponse response) {
		if (sysUser != null) {
			try {
                SysUser AdmUser = (SysUser) request.getSession().getAttribute(SESSION_KEY_USERINFO);
                if (AdmUser.getRoleId() != null && AdmUser.getRoleId().intValue() != 2) {
                    //非超级管理员隐藏密码
                    sysUser.setUserPwd("");
                }
				int count = sysUserService.updateByPrimaryKeySelective(sysUser);
				responseSendMsg(response, count + "");
			} catch (Exception e) {
				e.printStackTrace();
				responseSendMsg(response, "修改失败!");
			}
		}
	}

    /**
     * 删除用户
     * UserController.user_delete()<BR>
     * <P>Author :  fangxiaowen </P>  
     * <P>Date : 2016年10月8日 </P>
     * @param request
     * @param response
     */
    @RequestMapping(value = "user_delete",method = {RequestMethod.POST})
    public void user_delete(HttpServletRequest request, HttpServletResponse response) {
    	try {
    		int count = 0;
    		Integer id = Integer.valueOf(request.getParameter("id").toString());
    		count = sysUserService.deleteUserById(id);
    		responseSendMsg(response, count + "");
    	} catch (Exception e) {
    		e.printStackTrace();
    		responseSendMsg(response, "添加失败!");
    	}
    }
    
    
    /**
     * 跳转到用户编辑页面
     * UserController.to_user_edit()<BR>
     * <P>Author :  fangxiaowen </P>  
     * <P>Date : 2016年10月8日 </P>
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "to_user_edit",method = {RequestMethod.POST, RequestMethod.GET})
    public ModelAndView to_user_edit(HttpServletRequest request, HttpServletResponse response) {
    	
    	ModelAndView model = new ModelAndView("user/user_edit");
    	Integer id = Integer.valueOf(request.getParameter("id"));
    	
    	SysUser sysUser = sysUserService.queryUserById(id);
		model.addObject("sysUser", sysUser);
    	
    	List<SysRole> role_list = sysUserService.queryAllRole();
		model.addObject("role_list", role_list);
    	
    	List<SysMenu> menu_list = sysUserService.queryMenuList();
    	List<SysMenu> user_menu_list = sysUserService.queryUserMenuList(id);
    	for (SysMenu menu : menu_list) {
    		for (SysMenu user_menu : user_menu_list) {
    			if (StringUtil.isEquals(menu.getId(), user_menu.getId())) {
    				menu.setIsChecked("checked='checked'");
    				break;
				}
			}
		}
		model.addObject("menu_list", menu_list);
		model.addObject("user_menu_list", user_menu_list);
		List<StoreInfo> storeList = storeService.queryAllOnlineStore();
		request.setAttribute("storeList", storeList);
    	return model;
    }
    
    
    /**
	 * 跳转到修改密码
	 * UserController.to_user_list()<BR>
	 * <P>Author :  fangxiaowen </P>  
	 * <P>Date : 2016年9月27日 </P>
	 * @return
	 */
    @RequestMapping(value = "to_change_password")
    public ModelAndView to_change_password() {
    	ModelAndView model = new ModelAndView();
        model.setViewName("change-password");
        return model;
    }
    
    
    /**
     * 修改密码
     * UserController.change_password()<BR>
     * <P>Author :  fangxiaowen </P>  
     * <P>Date : 2016年10月14日 </P>
     * @param request
     * @param response
     */
    @RequestMapping(value = "change_password",method = {RequestMethod.POST, RequestMethod.GET})
    public void change_password(HttpServletRequest request, HttpServletResponse response) {
    	try {
    		String oldPwd = request.getParameter("oldPwd");
        	String newPwd = request.getParameter("newPwd");
    		int count = sysUserService.updatePassword(oldPwd, newPwd, getUserInfo().getId());
    		responseSendMsg(response, count + "");
    	} catch (Exception e) {
    		e.printStackTrace();
    		responseSendMsg(response, "修改密码失败，请检查原密码是否正确!");
    	}
    }

}
